package net.skyedu.service.system;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.skyedu.bean.UserInfo;
import net.skyedu.bean.UserList;
import net.skyedu.dao.UserInfoDao;
import net.skyedu.utils.Page;
import net.skyedu.utils.PageHelp;


public class ShowUser extends HttpServlet {

	private static final long serialVersionUID = 5952060001106180351L;

	/**
	 * Constructor of the object.
	 */
	public ShowUser() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * This method is called when a form has its tag value method equals to post.
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		HttpSession session=request.getSession();
		
		String userID=request.getParameter("USERID");
		String name=request.getParameter("username");
		String ssn=request.getParameter("ssn");
		String pagerMethod=request.getParameter("pagerMethod");
		String sta=request.getParameter("sta");
		
		if (pagerMethod != null) {
			name=(String)session.getAttribute("na");
			ssn=(String)session.getAttribute("ssn");
		}
//		int schId=0;
//		if (pagerMethod != null) {
//			name=(String)session.getAttribute("na");
//		}
//		if(schID!=null){
//			schId=Integer.parseInt(schID);
//			session.setAttribute("schId",schId);
//		}else{
//			Integer sch=(Integer)session.getAttribute("schId");
//			if(sch!=null)
//				schId=sch;			
//		}
		int userId=0;
		if(userID!=null){
			userId=Integer.parseInt(userID);
			session.setAttribute("uid",userId);
		}else{
			Integer ser=null;
			if(session.getAttribute("uid")!=null){
			  ser=(Integer)session.getAttribute("uid");
			  userId=ser;
			}else{
				session.removeAttribute("uid");
			    request.getRequestDispatcher("attend/error.jsp").forward(request, response);
				return;
			}
		}
		UserInfoDao userDao=new UserInfoDao();
		UserInfo ui=userDao.getSSN(userId);
//		System.out.println(ui.getDefaultDeptId());
		int totalRows=0;
		Page page=null;
		List<UserList> userlist=new ArrayList<UserList>();
		
		
		if(sta==null||!"1".equals(sta))
		{
			totalRows=userDao.getCountUser(ui.getDefaultDeptId(), name , ssn);
			page=PageHelp.getPager(request, totalRows);;
			userlist=userDao.getAllUser(page.getPageSize(), page.getStartRow(), ui.getDefaultDeptId(), name, ssn);
			request.setAttribute("page", page);
			request.setAttribute("userlist", userlist);
			request.getRequestDispatcher("attend/user/userBySch.jsp").forward(request, response);
		}else
		{
			totalRows=userDao.getCountUser( name , ssn);
			page=PageHelp.getPager(request, totalRows);;
			userlist=userDao.getAllUserSecond(page.getPageSize(), page.getStartRow(), ui.getDefaultDeptId(), name, ssn);
			request.setAttribute("page", page);
			request.setAttribute("userlist", userlist);
			 request.getRequestDispatcher("attend/user/showUser.jsp").forward(request, response);
		}
	
		session.setAttribute("na",name);
		session.setAttribute("ssn",ssn);
	
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
